﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Bean;
using BarCode.Common;
using System.Data;
using BarCode.Module;
using System.Data.OleDb;

namespace BarCode.AccessDao
{
    class ConfigMdbDao
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(ConfigMdbDao));

        private AccessHelper configsMdb;
        public ConfigMdbDao()
        {
            configsMdb = new AccessHelper(Consts.MDB_CONFIGS);
        }

        public void Release()
        {
            configsMdb.Release();
        }

        /// <summary>
        /// 获取IP配置信息
        /// </summary>
        /// <returns></returns>
        public IpAddressBean getIPAddressData()
        {

            DataSet retSet = configsMdb.Query("select * from ipaddress");
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    IpAddressBean retBean = new IpAddressBean();
                    retBean.SERVICE_IP = dt.Rows[0]["ServiceIPadd"].ToString();
                    retBean.PRINTER_NAME = dt.Rows[0]["PrinterName"].ToString();
                    retBean.DATABASE_NAME = dt.Rows[0]["DBname"].ToString();
                    retBean.JXJK_IP = dt.Rows[0]["jxjkIPadd"].ToString();
                    retBean.JXJK_DBNAME = dt.Rows[0]["jxjkDBname"].ToString();
                    retBean.JSQMP_IP = dt.Rows[0]["jsqmpIPadd"].ToString();
                    retBean.JSQMP_DBNAME = dt.Rows[0]["jsqmpDBname"].ToString();
                    if (dt.Columns.Contains("diskvalue"))
                    {
                        retBean.DISK_VALUE = dt.Rows[0]["diskvalue"].ToString();
                    }
                    return retBean;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 获取生产线编号
        /// </summary>
        /// <returns></returns>
        public string getLineNo()
        {
            DataSet retSet = configsMdb.Query("select lineno from line_name");
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    return dt.Rows[0]["lineno"].ToString();
                }
                else
                {
                    return "";
                }
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 删除非管理员的用户数据
        /// </summary>
        /// <returns></returns>
        public int deleteNotAdminUser()
        {
            return configsMdb.ExecuteSql("delete from users where user<>'administrator'");
        }

        /// <summary>
        /// 插入用户信息
        /// </summary>
        /// <param name="lstUser"></param>
        /// <returns></returns>
        public int insertUsers(List<UserBean> lstUser)
        {
            string insertSql = "insert into [Users] ([User], [Password], [Level], [lineno]) Values (@User, @Password, @Level, @lineno)";
            int retCnt = 0;
            foreach (UserBean userItem in lstUser)
            {
                OleDbParameter[] sqlParams = new OleDbParameter[4];
                sqlParams[0] = new OleDbParameter("User", userItem.User);
                sqlParams[1] = new OleDbParameter("Password", userItem.Password);
                sqlParams[2] = new OleDbParameter("Level", userItem.Level);
                sqlParams[3] = new OleDbParameter("lineno", userItem.lineno);
                retCnt += configsMdb.ExecuteSql(insertSql, sqlParams);
            }
            return retCnt;
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <returns></returns>
        public List<UserBean> getUsers()
        {
            List<UserBean> retList = new List<UserBean>();
            DataSet retSet = configsMdb.Query("select * from users order by User");
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow tmpRow in dt.Rows)
                    {
                        UserBean tmpInfo = new UserBean();
                        tmpInfo.User = Convert.ToString(tmpRow["User"]);
                        tmpInfo.Password = Convert.ToString(tmpRow["Password"]);
                        tmpInfo.Level = Convert.ToString(tmpRow["Level"]);
                        tmpInfo.lineno = Convert.ToString(tmpRow["lineno"]);
                        retList.Add(tmpInfo);
                    }
                }
            }
            return retList;
        }

        /// <summary>
        /// 通过用户名获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserBean getUserInfo(string userName)
        {
            OleDbParameter[] sqlParams = new OleDbParameter[1];
            sqlParams[0] = new OleDbParameter("User", userName);
            DataSet retSet = configsMdb.Query("select * from users where [User] = @User", sqlParams);
            if (retSet != null && retSet.Tables.Count > 0)
            {
                DataTable dt = retSet.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    DataRow tmpRow = dt.Rows[0];
                    UserBean tmpInfo = new UserBean();
                    tmpInfo.User = Convert.ToString(tmpRow["User"]);
                    tmpInfo.Password = Convert.ToString(tmpRow["Password"]);
                    tmpInfo.Level = Convert.ToString(tmpRow["Level"]);
                    tmpInfo.lineno = Convert.ToString(tmpRow["lineno"]);
                    return tmpInfo;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 保存生产线编号信息
        /// </summary>
        /// <returns></returns>
        public int saveLineInfo(LineInfo lineInfo)
        {
            // 删除原有信息
            configsMdb.ExecuteSql("delete from line_name");
            string insertSql = "insert into [line_name] ([lineno], [linename]) Values (@lineno, @linename)";
            OleDbParameter[] sqlParams = new OleDbParameter[2];
            sqlParams[0] = new OleDbParameter("lineno", lineInfo.lineNo);
            sqlParams[1] = new OleDbParameter("linename", lineInfo.lineName);
            return configsMdb.ExecuteSql(insertSql, sqlParams);

        }

        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <returns></returns>
        public int saveConfigInfo(string serverIp, string printerName, string dbName)
        {
            string updSql = "update ipaddress set  [ServiceIPadd] = @ServiceIPadd, PrinterName=@PrinterName, DBname=@DBname ";
            OleDbParameter[] sqlParams = new OleDbParameter[3];
            sqlParams[0] = new OleDbParameter("ServiceIPadd", serverIp);
            sqlParams[1] = new OleDbParameter("PrinterName", printerName);
            sqlParams[2] = new OleDbParameter("DBname", dbName);
            return configsMdb.ExecuteSql(updSql, sqlParams);
        }

    }
}
